
clear 
include "$CODE/pathnames"

* Ensure Stata looks in the local ado folder first
adopath ++ "`c(pwd)'/ado"
* Verify ado path order
adopath

********************************************************************************
**** Table 1: VK Penetration Effect Over Time, 2011–2012. Panel IV

use "$DATA/data_crosscity_analysis_final.dta", clear

global outtable="$REPLICATION/Tables_MainText/Table_1.xls"
global ivregtype="ivreg2"
global err="cluster(okato_reg)"
global SPcohortsnoDurov="logspbsu1 logspbsu3"
global control4="pop pop_2 pop_3 pop_4 pop_5 ac dp dm city2 logwage11 lA20_24_y2010 lA25_29_y2010 lA30_34_y2010 lA35_39_y2010  lA40_44_y2010 lA45_49_y2010 lA50_more_y2010 he2010_A20_24 he2010_A25_29 he2010_A30_34 he2010_A35_39 he2010_A40_44 he2010_A45_49 he2010_A50_more University_exists educ2002 ip_2011 logok2014 ef2010"
local controlall=""
forval i=5/7 {
	local y=95+(`i'-5)*4
	local y=`y'-int(`y'/100)*100
	local t="0`y'"
	local t=regexr("`t'","09","9")
	global control`i'="$control4 yabloko_`t' communists_`t' statusquo_`t' ldpr_`t' turnout_`t' against_`t'"
}	
local i=4
foreach varlist in "$control4" "$control5" "$control6" "$control7" {
	local tmp=""
	foreach var in `varlist' {
		local tmp="`tmp' `var'_t"
	}
	global timecontrol`i'="`tmp'"
	local i=`i'+1
}

local opt="replace"
xtset cityid week
foreach depvar in logprot dumprot {
	foreach time in "t t2 t3 t4 t5" {
		foreach control in "timecontrol4" "timecontrol5" "timecontrol6" "timecontrol7" {
			xtivreg2 `depvar' logspbsu1_t logspbsu3_t $`control' logoldprot_t `time' (logvk_t = logspbsu2_t) if week_year<201187, fe  $err 
			local FKP=e(widstat) 
			
			outreg2 using "$outtable", bracket drop(pop* lA* yabloko* communists* statusquo* ldpr* turnout*	against* ) `opt' label dec(4) nocons addstat("K-P F-stat", `FKP') cttop("Whole Sample") 
			local opt="append"
		}		
	}	
}

********************************************************************************
**** Table 2: Association between Social Capital and Protests, 2011–2012. Within-City Analysis

use "$DATA/data_crosscity_analysis_final.dta", clear
keep if pop>50

global outtable="$REPLICATION/Tables_MainText/Table_2.xls"
global ivregtype="ivreg2"
global err="cluster(okato_reg)"
global control4="pop pop_2 pop_3 pop_4 pop_5 logwage11 educ2002 educ ip_2011 ef2010"
local controlall=""
forval i=5/7 {
	local y=95+(`i'-5)*4
	local y=`y'-int(`y'/100)*100
	local t="0`y'"
	local t=regexr("`t'","09","9")
	global control`i'="$control4 yabloko_`t' communists_`t' statusquo_`t' ldpr_`t' turnout_`t' against_`t'"
}	
local i=4
foreach varlist in "$control4" "$control5" "$control6" "$control7" {
	local tmp=""
	foreach var in `varlist' {
		local tmp="`tmp' `var'_t"
	}
	global timecontrol`i'="`tmp'"
	local i=`i'+1
}
global time = "t t2 t3 t4 t5"

foreach depvar in logprot dumprot {
  foreach indepvar in ln_consumerscooperatives ln_NGO sc_trust Social_cap_Katz  {
		foreach control in "timecontrol4" {
			reghdfe `depvar' `indepvar'_t  $`control' $time , a(OKATO)  $err 
			outreg2 using "$outtable", bracket drop(pop* lA* yabloko* communists* statusquo* ldpr* turnout* against* $time ) ///
			`opt' label dec(4) nocons cttop("The first two weeks")

			local opt="append"
		}		
	}	
}	

